#! /bin/bash
#pSchedule
#Copyright 2007,2008,2009,2010,2011,2012,2013,2014,2015
#Sigmund Berglund

#------------------------------
#pSchedule is released under the GNU General Public License (GPL). You have the right to use and modify this software in any way you like, so long as any derivative works remain under a GPL license.
    
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.
#See the GNU General Public License homepage for more details. <http://www.gnu.org/licenses/>.
#------------------------------

VERSION="1.1.7"
export PROGPATH="/usr/local/pschedule"
export BROWSER="defaultbrowser"

export GTKDIALOG=gtkdialog
#gettext
export TEXTDOMAIN=pschedule
export OUTPUT_CHARSET=UTF-8


#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do #check -xft
		case $1 in
			-e) MODE='-edit';;
			-n) MODE='-new';;
			-s) SILENT=true;;
			-v|--version) echo pSchedule $VERSION; exit;;
			-h|--help) echo '
Usage: pschedule [Options(s)] [Command] [Task Name]

Options
  -e                Edit given task
  -n                Edit new task
  -h | --help       Show this help message
  -s                Do not show main window. Go straight to task editor
  -v | --version    Show pSchedule version'; exit;;
			*) 	export TASK="$1"; export TASKNAME="$2"
				shift; shift; break; break;;
		esac
		shift
		I=$[$I+1]
	done
done

mkdir -p /var/spool/cron/crontabs
if [ ! -L /var/local/cron -o ! -e /var/local/cron ] ; then
	rm -rf /var/local/cron 2>/dev/null
	ln -s ../../var/spool/cron /var/local/cron
fi

function enable_crond() {
	chmod +x /etc/init.d/crond
	/etc/init.d/crond restart
	#Did these really happen. Do user has permission.
	if ! ( busybox ps | grep -v "grep" | grep -q " crond" ) ; then #cron is not running
		FRAME="Error"; TXT2="$(gettext 'Cron deamon is not running. You need root-access to start cron')"; IMG="dialog-error"
		. "$PROGPATH"/box_ok
	fi
}
function disable_crond() {
	sh /etc/init.d/crond stop
	chmod -x /etc/init.d/crond
}
export -f enable_crond
export -f disable_crond

if [ -x /etc/init.d/crond ] ; then
	CROND_ENABLED="true"
else
	disable_crond
	CROND_ENABLED="false"
fi

#-------------------------------------------------------------------

#build treeitems for 'Add task' --- better have the delay at startup
COUNT=0; ITEMS_HOUR="$ITEMS_HOUR<item>$(gettext 'any')</item>"
while [ $COUNT -lt 24 ]; do
	if [ $COUNT -lt 10 ]; then COUNT=0"$COUNT"; fi #always 02 digits
	export ITEMS_HOUR="$ITEMS_HOUR<item>$COUNT</item>"
	COUNT=`echo "$COUNT + 1" | bc`
done
COUNT=0; ITEMS_MINUTE="$ITEMS_MINUTE<item>$(gettext 'any')</item>"
while [ $COUNT -lt 60 ]; do
	if [ $COUNT -lt 10 ]; then COUNT=0"$COUNT"; fi #always 02 digits
	export ITEMS_MINUTE="$ITEMS_MINUTE<item>$COUNT</item>"
	COUNT=`echo "$COUNT + 1" | bc`
done
COUNT=1; ITEMS_DAY="$ITEMS_DAY<item>$(gettext 'any')</item>"
while [ $COUNT -lt 32 ]; do
	export ITEMS_DAY="$ITEMS_DAY<item>$COUNT.</item>"
	COUNT=`echo "$COUNT + 1" | bc`
done
export ITEMS_WEEKDAY="$ITEMS_WEEKDAY
<item>$(gettext 'any')</item>
<item>$(gettext 'Mondays')</item>
<item>$(gettext 'Tuesdays')</item>
<item>$(gettext 'Wednesdays')</item>
<item>$(gettext 'Thursdays')</item>
<item>$(gettext 'Fridays')</item>
<item>$(gettext 'Saturdays')</item>
<item>$(gettext 'Sundays')</item>"
export ITEMS_MONTH="$ITEMS_MONTH
<item>$(gettext 'any')</item>
<item>$(gettext 'January')</item>
<item>$(gettext 'February')</item>
<item>$(gettext 'March')</item>
<item>$(gettext 'April')</item>
<item>$(gettext 'May')</item>
<item>$(gettext 'June')</item>
<item>$(gettext 'July')</item>
<item>$(gettext 'August')</item>
<item>$(gettext 'September')</item>
<item>$(gettext 'October')</item>
<item>$(gettext 'November')</item>
<item>$(gettext 'December')</item>"
#---

export about='
<window title="pSschedule - about" icon-name="pschedule">
 <notebook labels="About|License|Credits">
  <frame>
   <text><label>""</label></text>'"
   <text use-markup=\"true\"><label>\"<b><span size='"'x-large'"'>pSchedule</span> $VERSION</b>\"</label></text>
   <text use-markup=\"true\"><label>Sigmund Berglund, Copyright 2007-2014</label></text>
   <text><label>\"\"</label></text>
   <button>
    <label>\"http://www.murga-linux.com/puppy/viewtopic.php?t=22166\"</label>
    <action>$BROWSER http:\/\/www.murga-linux.com/puppy/viewtopic.php?t=22166 &</action>
   </button>
  </frame>
  <frame>
   <text use-markup=\"true\"><label>\"pSchedule is released under the <b>GNU General Public License</b> (GPL). You have the right to use and modify this software in any way you like, so long as any derivative works remain under a GPL license.\"</label></text>
   <text use-markup=\"true\"><label>\"This program is distributed in the hope that it will be useful, but <b><span color='"'red'"'>WITHOUT ANY WARRANTY</span></b>. See the GNU General Public License homepage for more details.\"</label></text>
   <hbox>
    <button label=\"http://www.gnu.org/licenses/\"><action>$BROWSER http:\/\/www.gnu.org\/licenses\/</action></button>
   </hbox>
  </frame>
  <frame>
   <edit><default>\"Translators:
German - Lutz-Ulrich Ringel (L18L)

Others:
  jake_take for improvements.
  HairyWill for advanced bash help.
  mcewanw for cli options
\"</default></edit>
  </frame>
 </notebook>
</window>"

S='
<window title="pSchedule '$VERSION' - '$(gettext 'Task on time')'" icon-name="pschedule">
 <vbox>
  <hbox>
   <button space-expand="false" space-fill="false" tooltip-text="'$(gettext 'Launch settings window')'">
    <variable>BUTTON_ADD</variable>
    <label>'$(gettext 'Schedule new task')'</label>'
	[ -f /usr/share/pixmaps/puppy/clock_interval.svg ] && S=$S'<input file>/usr/share/pixmaps/puppy/clock_interval.svg</input>' || S=$S'<input file stock="gtk-add"></input>'
	S=$S'<height>22</height>
    <action>. '$PROGPATH'/func_new</action> 
    <action>. '$PROGPATH'/func_cron_to_human</action>
    <action>refresh:TREE</action>
   </button>
   <text space-expand="true" space-fill="true"><label>""</label></text>
   <button>
    <variable>BUTTON_EDIT</variable>
    <label>'$(gettext 'Edit')'</label>
    <input file stock="gtk-edit"></input>
    <visible>disabled</visible>
    <action>. '$PROGPATH'/func_new -edit</action>
    <action>. '$PROGPATH'/func_cron_to_human</action>
    <action>refresh:TREE</action>
   </button>
   <button>
    <variable>BUTTON_DEL</variable>
    <label>'$(gettext 'Delete')'</label>
    <input file stock="gtk-delete"></input>
    <visible>disabled</visible>
    <action>. '$PROGPATH'/func_delete</action>
    <action>. '$PROGPATH'/func_cron_to_human</action>
    <action>refresh:TREE</action>
   </button>
   <button>
    <variable>BUTTON_RUN</variable>
    <label>'$(gettext 'Execute now')'</label>
    <input file stock="gtk-execute"></input>
    <visible>disabled</visible>
    <action>. '$PROGPATH'/func_run</action>
   </button>
  </hbox>
  
  <tree rules_hint="true" exported_column="0">
   <width>500</width><height>200</height>
   <label>"'$(gettext 'Task')'|'$(gettext 'Schedule')'|'$(gettext 'Cron command')'"</label>
   <variable>TREE</variable>
   <input>cat /tmp/pschedule-treeitems</input>
   <action signal="button-release-event">enable:BUTTON_DEL</action>
   <action signal="button-release-event">enable:BUTTON_EDIT</action>
   <action signal="button-release-event">enable:BUTTON_RUN</action>
  </tree>
  <hbox>
   <checkbox active="'${CROND_ENABLED}'">
      <label>'$(gettext 'Enable crond service')'</label>
      <default>false</default>
      <action>if true enable_crond &</action>
      <action>if false disable_crond </action>
   </checkbox>
   <text space-expand="true" space-fill="true"><label>""</label></text>
   <button tooltip-text="'$(gettext 'About')'">
    <input file stock="gtk-about"></input>
    <action>launch:about</action>
   </button>
   <button tooltip-text="'$(gettext 'Quit')'">
    <input file stock="gtk-quit"></input>
    <action type="exit">EXIT</action>
   </button>
  </hbox>

 </vbox>
</window>'

export pschedule="$S"
. $PROGPATH/func_cron_to_human
[ "$SILENT" ] && export TREE="$TASK" #no tree with no gui.
[ "$TASK" ] && . $PROGPATH/func_new "$MODE" "$TASK" "$TASKNAME" &

if [ ! "$SILENT" ]; then
	I=$IFS; IFS=""
	for STATEMENTS in  $($GTKDIALOG -p pschedule); do
		eval $STATEMENTS
	done
	IFS=$I
	case $EXIT in
		*)	rm /tmp/pschedule*
			exit;;
	esac
fi

### END ###
